$(function() {
	function format(data) {
		return data.google_address ;
	}
	$("#search_address").autocomplete('/json/search_address', {
		multiple: false,
		dataType: "json",
		multipleSeparator: "| ",
		parse: function(data) {
			return $.map(data, function(row) {



				return {
					data: row,
					value: row.google_address,
					result: row.google_address
				}
			});
		},
		formatItem: function(item) {
			return format(item);
		}
	}).result(function(e, item) {
	//$("#content").empty();
	var toclean = [ 'country_name_code' ,
        'country_name' ,
        'administrative_area_name' ,
        'sub_administrative_area_name' ,
        'locality_name' ,
        'thoroughfare_name',
        'google_address',
        'postal_code_number',
        'google_lng',
        'google_lat'];
    for (var key in toclean ){
    	var tag = "#"+toclean[key];
		$(tag).val('');
    };
    $('#google_address_legend').html(item['google_address']);
	for (var key in item) {
		var value = item[key];
		var tag = "#"+key;
		$(tag).val(value);
		/*$._p()
     		._label_()
     		.attr("for", key)
            .text(key)
            ._br_()
            ._input_()
            .addClass("text")
            .attr("id", key)
            .attr("name", key)
            .attr("value", value)
		.p_().appendTo("#content2");
		*/
        			}
	});
});
